Skip to content

Conversation

@Orest-Divintari
Copy link
Contributor

@Orest-Divintari Orest-Divintari commented Aug 8, 2025

There is already a rule to convert foreach that contains a break statement with array_any. This pr is to add support to the existing rule to convert foreach with early return to array_any

There is already a rule to convert foreach that contains a break statement with array_any.
This new rule adds support for foreach that return early with true
@samsonasik
Copy link
Member

On return need to be supported on downgrade rule first, which currently only on Expression

https://github.com/rectorphp/rector-downgrade-php/blob/3ca042a0b6cc60d3ce8e516cd45f82677e1fd843/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector.php#L31

The ExprInTopStmtMatcher may need to be used for that for allow more usecases, see comment

@samsonasik
Copy link
Member

@Orest-Divintari
Copy link
Contributor Author

Actually, you may can improve existing rule

https://github.com/rectorphp/rector-src/blob/main/rules/Php84/Rector/Foreach_/ForeachToArrayAnyRector.php

i can do that sure, i wanted to check with you what is your preference
i started with a separate rule to not mix up anything yet

@samsonasik
Copy link
Member

Would be better to improve downgrade rule first, as we most got issue after downgraded a target code

@Orest-Divintari
Copy link
Contributor Author

i haven't managed to set it up locally properly and i can't run tests

for some reason when i do composer update it gets stuck at this stage

- Applying patches for illuminate/container https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/illuminate-container-container-php.patch (0)

any idea what might be the issue ? @samsonasik

@samsonasik
Copy link
Member

You may need to install gpatch, see troubleshooting note

https://github.com/symplify/vendor-patches/?tab=readme-ov-file#troubleshooting

@samsonasik
Copy link
Member

samsonasik commented Aug 8, 2025

@Orest-Divintari
Copy link
Contributor Author

i'll do also the downgrade rule

@samsonasik
Copy link
Member

samsonasik commented Aug 9, 2025

Downgrade rule already covered for return :)

@Orest-Divintari
Copy link
Contributor Author

does it cover also this code ?

 foreach ($animals as $animal) {
            if (str_starts_with($animal, 'c')) {
                return true;
            }
        }
        return false;

@samsonasik
Copy link
Member

samsonasik commented Aug 9, 2025

Downgrade is cover direct return function, change this:

return array_any($animals, fn($animal) => str_starts_with($animal, 'c'));

into:

$found = false;
foreach ($animals as $animal) {
    if (str_starts_with($animal, 'c')) {
        $found = true;
         break;
    }
}
return $found;

see fixture https://github.com/rectorphp/rector-downgrade-php/pull/300/files#diff-29bc31c0c16a251c20583c193cd7e88ca450703044d2c33ea28727a38d3cdc30

equivalent code needs exists when upgraded code downgraded, on rector usecase, rector downgraded to php 7.4, that's downgrade rule usage needs after new upgrade feture added, other lib/project that downgrade code after upgraded via rector needs that as well.

@samsonasik samsonasik changed the title Add ForeachWithReturnToArrayAnyRector Allow on return on ForeachToArrayAnyRector Aug 11, 2025
@samsonasik samsonasik changed the title Allow on return on ForeachToArrayAnyRector [Php84 Allow on return on ForeachToArrayAnyRector Aug 11, 2025
@samsonasik samsonasik changed the title [Php84 Allow on return on ForeachToArrayAnyRector [Php84] Allow on return on ForeachToArrayAnyRector Aug 11, 2025
@samsonasik
Copy link
Member

Okay, let's give it a try, thank you @Orest-Divintari

@samsonasik samsonasik merged commit 25f099f into rectorphp:main Aug 11, 2025
47 checks passed
@github-actions
Copy link
Contributor

This pull request has been automatically locked because it has been closed for 150 days. Please open a new PR if you want to continue the work.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 11, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants